package com.example.school.utils.config;

import com.example.school.pojo.Orders;
import com.example.school.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;

@Configuration
@Transactional
public class CheckOrdersTimeConfig {

    @Autowired
    private OrdersService ordersService;

    @PostConstruct
    /*
    ***开启需谨慎
    * 每两秒自检一次，删除存在时间大于等于一个月的订单
     */
//    @Scheduled(cron = "0/2 * * * * ?")
    public void CheckOrdersTime(){
        List<Orders> checkOrdersList = ordersService.list();
        for (Orders checkOrders : checkOrdersList) {
            Long ordersTime=checkOrders.getOrdersMsec()+2629800000l;
            if (ordersTime>=System.currentTimeMillis()) {
                boolean checkRemove = ordersService.removeById(checkOrders.getOrdersId());
                if (!checkRemove) {
                    throw new RuntimeException("有需要删除的订单删除出现异常");
                }
            }
        }
    }


}
